package cn.ysl.management.web;

import cn.ysl.management.comm.util.JsonResult;
import cn.ysl.management.service.exception.*;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseUserController {
    public static final int OK = 200;

    /** 统一异常处理 */
    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> result = new JsonResult<>(e);
        if (e instanceof AdminDuplicatedException) {
            result.setState(4000);
            result.setMessage("管理员已经被占用");
        } else if (e instanceof InsertException) {
            result.setState(5000);
            result.setMessage("注册是产生未知的异常");
        } else if (e instanceof AdminNotFoundException) {
            result.setState(5001);
            result.setMessage("管理员数据不存在");
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(5002);
            result.setMessage("账户密码不匹配");
        } else if (e instanceof UpdateException) {
            result.setState(5003);
            result.setMessage("更新数据时产生未知异常");
        }

        return result;
    }
}
